package com.yimi.yinhepda.views.stockCheck_module;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.pda.uce.commom.util.LogUtil;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.RecycleAdapter.StockInLocalDeptAdapter;
import com.pda.ym.base.BaseTitleActivity;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.entity.bean.StockInLocalDeptChildWaybillEntity;
import com.yimi.yinhepda.entity.bean.StockInLocalDeptEntity;
import com.yimi.yinhepda.entity.request.StockInLocalDeptRequest;
import com.pda.ym.net.NetRequest;
import com.pda.ym.utils.Constants;
import com.pda.uce.commom.util.MyDialog;
import com.pda.ym.utils.SoundVibratorManager;
import com.pda.ym.utils.StringUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 入库至本部门界面.
 *
 * @author 000432
 * @date 2018/6/4 0004
 */

public class StockInLocalDeptActivity extends BaseTitleActivity {

    private RecyclerView rvStockInListView;
    private StockInLocalDeptAdapter adapter;
    private List<StockInLocalDeptChildWaybillEntity> dataList;
    private final int limtScanNnum = 50;

    @Override
    public String getPageName() {
        return "入库至本部门";
    }

    public StockInLocalDeptActivity() {
        super(true);
    }

    @Override
    public int setContentViewId() {
        return R.layout.activity_stock_in_local_dept;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        rvStockInListView =  findViewById(R.id.rv_in_stock_list);
        findViewById(R.id.btn_stock_in_back).setOnClickListener(this);
        findViewById(R.id.btn_stock_in).setOnClickListener(this);
    }

    private void initData() {
        dataList = new ArrayList<>();
        adapter = new StockInLocalDeptAdapter(this, dataList);
        rvStockInListView.setLayoutManager(
                new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rvStockInListView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_stock_in_back:
                finish();
                break;
            case R.id.btn_stock_in:
                submitRequest();
                break;
            default:
                break;
        }
    }

    @Override
    protected void handleBarCode(String result) {
        if (result == null) {
            SoundVibratorManager.playSound(2);
            showToast("运单号扫描失败");
        } else {
            doScan(result);
        }
    }

    /**
     * 处理扫描结果.
     */
    private void doScan(String result) {
        if (dataList != null && dataList.size() >= limtScanNnum) {
            //限制扫描件数为50
            MyDialog.showAlertDialog(this, "提示", "请先点击入库！", "确定");
            SoundVibratorManager.playSound(2);
            return;
        }

        if (!StringUtils.isWaybillNo(result) && !StringUtils.isPackgeNo(result)) {
            SoundVibratorManager.playSound(2);
            showToast("单号或包号不规范!");
            return;
        }

        for (StockInLocalDeptChildWaybillEntity entity : dataList) {
            if (result.equals(entity.getChildWaybillNo())) {
                showToast("运单已扫描！");
                SoundVibratorManager.playSound(2);
                return;
            }
        }

        dataList.add(new StockInLocalDeptChildWaybillEntity(result));
        adapter.notifyDataSetChanged();
        rvStockInListView.smoothScrollToPosition(adapter.getItemCount() - 1);
        SoundVibratorManager.playSound(1);
    }

    /**
     * 入库提交请求.
     */
    private void submitRequest() {
        if (dataList == null || dataList.size() == 0) {
            showToast("无运单提交！");
            SoundVibratorManager.playSound(2);
            return;
        }

        showPgDlg("数据提交中...");
        List<StockInLocalDeptEntity> masterEntity = new ArrayList<>();
        StockInLocalDeptRequest inLocalDeptRequest = new StockInLocalDeptRequest();
        //获取主单号
        Map<String, String> waybillNoMap = new HashMap<>(1);
        for (StockInLocalDeptChildWaybillEntity childEntity : dataList) {
            String waybillNo = childEntity.getChildWaybillNo().substring(0, 12);
            waybillNoMap.put(waybillNo, waybillNo);
        }
        //遍历主单map
        for (String waybillNo : waybillNoMap.values()) {
            //添加子单号
            List<StockInLocalDeptChildWaybillEntity> childWaybillEntities = new ArrayList<>();
            for (StockInLocalDeptChildWaybillEntity childEntity : dataList) {
                if (childEntity.getChildWaybillNo().startsWith(waybillNo)) {
                    childWaybillEntities.add(
                            new StockInLocalDeptChildWaybillEntity(childEntity.getChildWaybillNo()));
                }
            }
            //添加主单号
            masterEntity.add(new StockInLocalDeptEntity(waybillNo, childWaybillEntities));
        }
        inLocalDeptRequest.setListVo(masterEntity);

        new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                .setResponsClazz(Object.class)
                .setUrl(Constants.URL_INVENTORY_INTO)
                .setRequestObject(inLocalDeptRequest)
                .setConnTimeOut(5 * 1000)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        dismissPgDlg();
                        showToast(msg);
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        dismissPgDlg();
                        showToast("入库成功！");
                        dataList.clear();
                        adapter.notifyDataSetChanged();
                        SoundVibratorManager.playSound(1);
                    }
                });
    }

    @Override
    public boolean showBack() {
        return false;
    }

    @Override
    public void setBack() {

    }

    @NotNull
    @Override
    public String setTitle() {
        return getPageName();
    }

}
